const axios = require("axios")
const fs = require("fs")
const path = require("path")
const sharp = require("sharp")
const { workPath } = require("../config")

// const webpUrl =
//   "https://review-2.pddpic.com/review3/review/2023-05-03/fcb754dd-10d2-4719-9073-823407cc564d.jpeg?imageView2/2/w/1300/q/80/format/webp"
// // 保存为JPG图片的路径
// // 保存图片的路径（这里保存为JPG格式）

// 保存为JPG图片的路径
// 保存图片的路径（这里保存为JPG格式）
let jpgPath, s
let writeStream
const downloadImage = async (imgs = [], url) => {
  for (let index = 0; index < imgs.length; index++) {
    const webpUrl = imgs[index];
    let s = path.join(workPath, `${url}`)
    jpgPath = path.join(s, `${index}.jpg`)
    // return
    const response = await axios({
      method: "get",
      url: webpUrl,
      responseType: "arraybuffer",
    })

    const buffer = Buffer.from(response.data, "binary")
    // 同步写入文件
    console.log("保存的图片：", jpgPath)
    fs.writeFileSync(jpgPath, buffer) 
  }
}
module.exports = downloadImage
